<div id="tmp">
    <div class="col-md-offset-4 col-md-5">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">添加管理员账号</h3>
            </div>
            <form role="form" onsubmit="return false;">
                <!-- /.box-header -->
                <div class="box-body">
                    <div class="form-group" onsubmit="return false;">
                        <label>登录账号</label>
                        <input name="username" type="text" class="form-control" placeholder="登录密码 ...">
                        <span style="color: red;" class="help-block" v-text="e_u"></span>
                    </div>
                    <div class="form-group" onsubmit="return false;">
                        <label>用户昵称</label>
                        <input name="nickname" type="text" class="form-control" placeholder="登录密码 ...">
                        <span style="color: red;" class="help-block" v-text="e_n"></span>
                    </div>
                    <div class="form-group" onsubmit="return false;">
                        <label>登录密码</label>
                        <input onkeyup="pw_1(this)" name="password" type="password" class="form-control" placeholder="登录密码 ...">
                        <span style="color: red;" class="help-block" v-text="pw_1"></span>
                    </div>

                    <div class="form-group">
                        <label>再次输入</label>
                        <input onkeyup="pw_2(this)" value="" type="password" class="form-control" placeholder="再次输入 ...">
                        <span style="color: red;" class="help-block" v-text="pw_2"></span>
                    </div>
                </div>
                <!-- /.box-body -->
                <div class="box-footer text-center">
                    <button type="reset" class="btn btn-info">重置</button>
                    <button type="button" onclick="updatePassword()" class="btn btn-info">修改</button>
                </div>
            </form>
        </div>
        <!-- /.box -->
    </div>
</div>
<script>
    /*显示dialog的js
     qiao.bs.dialog({
     id : 'showUser',
     url :'./showUser.htm',
     head:false,
     foot:false,
     backdrop: true,
     big : true
     },function () {
     return true;
     });
     */
    var tmp = new Vue({
        el: '#tmp',
        data: {img:'',pw_1:'',pw_2:'',e_u:'',e_n:''},
        methods:{
        }
    });

    var pw_1 = function (obj) {
        if (!verifyStrLength($(obj).val(), 5,12)) {
            tmp.pw_1 = '密码长度5-12';
        } else {
            tmp.pw_1 = '';
        }
    }

    var pw_2 = function (obj) {
        if ($(obj).val() != $('input[name=password]').val()) {
            tmp.pw_2 = '两次密码不相同';
        } else {
            tmp.pw_2 = '';
        }
    }

    var updatePassword = function () {
        var info = {
            userName:$.trim($('input[name=username]').val()),
            nickName:$.trim($('input[name=nickname]').val()),
            pwd:$.trim($('input[name=password]').val())
        }
        if(isEmpty(info.userName) || isEmpty(info.nickName)
            ||isEmpty(info.pwd)){
            alert('参数类型不匹配……');
            return;
        }
        if(tmp.pw_1.length == 0 && tmp.pw_2.length == 0){
            if(isEmpty($($("input[type=password]")[0]).val())
                || isEmpty($($("input[type=password]")[1]).val())){
                tmp.pw_2 = '密码不能为空';
                tmp.pw_1 = '密码不能为空';
                return;
            }else{
                info.pwd = hex_md5(info.pwd);
                $.ajax({
                    type:'post',
                    url:'./addMasterAccount',
                    data:info,
                    success:function (result) {
                        if(result){
                            $("#addMaster").modal('hide');
                            delete data.info.wd;
                            toSearch(data.info);
                            qiao.bs.alert('成功添加管理员……');
                        }else{
                            tmp.e_u = '已存在同名账号……';
                        }
                    }
                });
            }
        }else{
            tmp.pw_1 = '密码不符合要求……';
        }
    }

    $('input[type=password]').on('keyup',function (e) {
        if(e.keyCode == 13){
            updatePassword();
        }
    });

    $('input[name=username]').on('keyup',function (e) {
       if(isEmpty(this.value)){
           tmp.e_u = '账号不能为空';
           return;
       }
       else tmp.e_u = '';
       if(!verifyStrLength(this.value,5,12)){
           tmp.e_u = '账号长度范围5-12';
           return;
       }
       else tmp.e_u = '';

       if(!verify(this.value,'username')){
           tmp.e_u = '账号只能由数字、英文、下划线组成';
           return;
       }
       else tmp.e_u = '';
    });

    $('input[name=nickname]').on('keyup',function (e) {
        if(isEmpty(this.value)){
            tmp.e_n = '昵称不能为空';
            return;
        }
        else tmp.e_n = '';
        if(!verifyStrLength(this.value,3,20)){
            tmp.e_n = '昵称长度范围3-20';
            return;
        }
        else tmp.e_n = '';
        if(!verify(this.value,'nickname')){
            tmp.e_n = '用户昵称不符合规范';
            return;
        }
        else tmp.e_n = '';
    })

    $(function () {
        var style = 'position: fixed; top: 0px; display: block; padding-left: 17px;';
        $("div[role=dialog]").attr("style", style);
        $("div.modal-content").removeClass('modal-content');
    });


</script>
